home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 881 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.1 KB

  1. From: jdmorris@ix.netcom.com (Jason D. Morris)
  2. Message-ID: <31599c95.4890732@nntp.ix.netcom.com>
  3. X-Original-Date: Wed, 27 Mar 1996 19:54:40 GMT
  4. Path: in1.uu.net!bounce-back
  5. Date: 27 Mar 96 21:44:38 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: Suggestion to the C++ standard
  9. Organization: Netcom
  10. References: <4jatnm$s9b@post.tau.ac.il>
  11. X-Netcom-Date: Wed Mar 27 11:53:46 AM PST 1996
  12. X-Newsreader: Forte Agent .99d/32.182
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBFAgUBMVm22OEDnX0m9pzZAQFqSQF/VxDH9TfUGMpUtczlgWYYgtqNL0GmlChy
  15.     IYteF4Q4SVBUuWg/fX9YjrDV6Zx027mR
  16.     =5Leu
  17.  
  18. >C++ is an OO language, and of course supports encapsulation.
  19. >Problem is - there is no standard way to add access methods.
  20. >For example:
  21. >class foo
  22. >{
  23. >public:
  24. >    SetMember( Type aMember );
  25. >    Type Member( );
  26. >private:
  27. >    Type Member;
  28. >};
  29.  
  30. #ifndef PROPERTY_H__
  31. #define PROPERTY_H__
  32.  
  33. // The property class controls access to a captive variable.
  34. // It captures the get/set pattern.  This class first appeared
  35. // in the November/December 1995 Vol. 7 / No. 9 Issue of C++ Report
  36. // in the article Patterns in Practice: A property template for C++
  37. // by Colin Hastie.
  38.  
  39. template < class T >
  40. class property
  41. {
  42. private:
  43.     T content;  // captive variable
  44.     
  45.     // non-implemented non-accessible assignment operator
  46.     void operator =( const property< T >& );
  47.  
  48. public:
  49.     // ctors
  50.     property() {}
  51.     property< T >( const T& t )
  52.         : content( t )
  53.     {}
  54.  
  55.     // accessors
  56.     T operator()() const   // get
  57.     { return content; }
  58.  
  59.     void operator()( const T& t )  // set
  60.     { content = t; }
  61. };
  62.  
  63. #endif
  64.  
  65.  
  66. To use this class you'd do something like this...
  67.  
  68. class foo
  69. {
  70. public:
  71.     property< int > foo_number;
  72. };
  73. ---
  74. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  75. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  76. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  77. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  78. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  79.